/*
 *  Copyright 2009 Matthieu Chase Heimer
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.

 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
package com.macro10.xroads.commands.filters;

import com.macro10.switchboard.AbstractCommand;
import com.macro10.switchboard.Request;
import com.macro10.switchboard.Session;
import com.macro10.switchboard.service.UserTransaction;
import com.macro10.xroads.entity.Avatar;
import com.macro10.xroads.service.AvatarService;

/**
 *
 * @author chase
 */
public class InWorldFilter extends AbstractCommand {

    @Override
    public ExecuteResult execute(Request request, Session session) {
        String username = (String) session.getAttribute("username");
        String avatarName = (String) session.getAttribute("avatar");

        UserTransaction ut = null;
        try {
            boolean filter = false;
            ut = new UserTransaction();
            AvatarService avatarService = new AvatarService(ut);

            Avatar avatar = avatarService.getAvatar(avatarName);
            if (avatar == null) {
                session.writeError(request.getURL() + ":avatar.not.found");
                filter = true;
            } else if (avatar.getAccount() == null) {
                session.writeError(request.getURL() + ":avatar.missing.account");
                filter = true;
            } else if (!avatar.getAccount().isOnline()) {
                session.writeError(request.getURL() + ":account.not.online");
                filter = true;
            } else if (!avatar.getAccount().getClientId().equals(username)) {
                session.writeError(request.getURL() + ":avatar.not.yours");
                filter = true;
            } else if (!avatar.isOnline()) {
                session.writeError(request.getURL() + ":avatar.not.online");
                filter = true;
            }
            if (avatar.getArea() == null) {
                session.writeError(request.getURL() + ":avatar.missing.area");
                filter = true;
            }
            ut.commit();
            if (filter) {
                return ExecuteResult.START_POST;
            } else {
                return ExecuteResult.CONTINUE;
            }
        } catch (Exception e) {
            session.writeException(request.getURL() + ":in.world.check.failed", e);
            if (ut != null) {
                ut.rollback();
            }
            return ExecuteResult.START_POST;
        }
    }
}
